package liecai_class.day03;

import java.util.Iterator;

/**
 * 自定义链表类
 */
public class MySingleList<T> implements Iterable<Node<T>> {
    private Node<T> head;
    private int size;

    public MySingleList(T ... datas) {
        if(datas.length<=0){
            throw new RuntimeException("链表初始数据不能为空!");
        }
        Node<T> preNode = null;
        for(T data : datas){
            Node<T> nowNode = new Node<T>(data,null);
            if(preNode == null){
                head = nowNode;
            }else{
                preNode.setNext(nowNode);
            }
            preNode = nowNode;
            size++;
        }
    }

    /**
     * 获取链表的长度
     */
    public int size(){
        return size;
    }

    /**
     * 获取指定位置的节点
     */
    public Node get(int index){
        if(index<0 || index>=size){
            throw new RuntimeException("链表访问越界！");
        }
        Node nowNode = null;
        for(int i =0;i<=index;i++){
            if(i == 0){
                nowNode = head;
            }else{
                nowNode = nowNode.getNext();
            }
        }
        return nowNode;
    }

    /**
     * 判断链表中是否包含指定元素
     */
    public boolean contains(T t){
        for(Node<T> node : this){
            if(t == node.getData() || t.equals(node.getData())){
                return true;
            }
        }
        return false;
    }

    /**
     * 获取迭代器
     */
    @Override
    public Iterator<Node<T>> iterator() {
        return new Iterator<Node<T>>() {
            Node<T> nowNode = head;
            @Override
            public boolean hasNext() {
                return nowNode != null;
            }

            @Override
            public Node<T> next() {
                try{
                    return nowNode;
                }finally {
                    nowNode = nowNode.getNext();
                }
            }
        };
    }

    /**
     * 头部插入新元素
     */
    public void addFirst(T t){
        Node<T> newHead = new Node<T>(t,head);
        head = newHead;
        size++;
    }

    /**
     * 尾部插入新元素
     */
    public void addLast(T t){
        Node<T> newTail = new Node<T>(t,null);
        if(size<=0){
            head = newTail;
        }else{
            Node<T> oldTail = get(size - 1);
            oldTail.setNext(newTail);
        }
        size++;
    }

    /**
     * 任意位置插入新元素
     */
    public void addIndex(T t,int index){
        if(index<0 || index>size){
            throw new RuntimeException("插入位置无效!");
        }
        if(index == 0){
            addFirst(t);
        }else if(index == size){
            addLast(t);
        }else{
            Node nextNode = get(index);
            Node preNode = get(index-1);
            Node newNode = new Node(t,nextNode);
            preNode.setNext(newNode);
        }
        size++;
    }

    /**
     * 修改元素
     */
    public void update(T t,int index){
        if(index<0 || index>=size){
            throw new RuntimeException("要修改的元素位置不正确!");
        }
        Node<T> nowNode = get(index);
        nowNode.setData(t);
    }

    /**
     * 删除指定位置元素
     */
    public void remove(int index){
        if(index<0 || index>=size){
            throw new RuntimeException("要删除的索引位置不存在!");
        }
        if(index == 0){
            Node<T> newHead = head.getNext();
            head.setNext(null);
            head = newHead;
        }else if(index == size - 1){
            Node preNode = get(index - 1);
            preNode.setNext(null);
        }else{
            Node nowNode = get(index);
            Node preNode = get(index-1);
            preNode.setNext(nowNode.getNext());
            nowNode.setNext(null);
        }
        size--;
    }

    /**
     * 清空链表
     */
    public void clear(){
        head = null;
        size = 0;
    }

    @Override
    public String toString() {
        return head.toString();
    }
}